var app=angular.module("myApp",[]);
app.controller("myController",function($http,$scope){
	$http({
		method : 'POST',
		url : '/test/AllUser'
	}).then(function successCallback(response) {
	
		$scope.rs = response.data.ls;
		
	}, function errorCallback(response) {

	});
	$scope.id="";
	$scope.name="";
	$scope.pwd="";
	$scope.email="";
	$scope.edit=true;
	$scope.error = false;
	$scope.incomplete=true;
	$scope.delete1=function(id){
	//	alert(id);
		$http({
			method : 'POST',
			url : '/test/delete',
			params : {
				'id' :id					
			}
		}).then(function successCallback(response) {
		
			$http({
				method : 'POST',
				url : '/test/AllUser'
			}).then(function successCallback(response) {
			
				$scope.rs = response.data.ls;
				
			}, function errorCallback(response) {

			});
			
		}, function errorCallback(response) {

		});
	}
	$scope.update=function(id){
		//alert(id);
		var a=id;
		$http({
			method : 'POST',
			url : '/test/informationid',
			params : {
				'id1' :id					
			}
		}).then(function successCallback(response) {
			$scope.u = response.data.u;
			
			if (a == 0) {
				$scope.edit = true;
				$scope.incomplete = false;
				$scope.id='';
				$scope.name = '';
				$scope.pwd = '';
				$scope.email = '';
			
			} else {
				$scope.edit = false;
				$scope.incomplete = false;
				$scope.id = $scope.u.id;
				$scope.name = $scope.u.name;
				$scope.pwd = $scope.u.pwd;
				$scope.email= $scope.u.email;
			}
			
			
		}, function errorCallback(response) {

		});
		
	}
	$scope.save=function(){
		if($scope.edit){
		//	alert("123");
		//	alert($scope.name);
		$http({
			method : 'POST',
			url : '/test/insert',
				params:{name:$scope.name,pwd:$scope.pwd,email:$scope.email}
		}).then(function successCallback(response) {
		
			if(response.data.result=="Y"){
				$http({
					method : 'POST',
					url : '/test/AllUser'
				}).then(function successCallback(response) {
				
					$scope.rs = response.data.ls;
					
				}, function errorCallback(response) {

				});
				
			}else{
				alert("用户名已存在");
			}
			
		}, function errorCallback(response) {

		})
	}else{
		//alert("1234");
		$http({
			method : 'POST',
			url : '/test/update',
				params:{id:$scope.id,name:$scope.name,pwd:$scope.pwd,email:$scope.email}
		}).then(function successCallback(response) {
			//alert($scope.id);
			if(response.data.result=="Y"){
				$http({
					method : 'POST',
					url : '/test/AllUser'
				}).then(function successCallback(response) {
				
					$scope.rs = response.data.ls;
					
				}, function errorCallback(response) {

				});
			}else{
				alert("用户名不能重复");
			}
			
			
			
		}, function errorCallback(response) {

		});
		
	}
		
}
	
	
	
	
})